@commodo/name
Decorates a function (and its instances) with a name, that can be used for various purposes.
For example, the @commodo/fields-storage
package relies on the name in order to determine the database table in which the data will be saved.
Usage
import { withName } from "@commodo/name";
import { compose } from "ramda";
const User = compose(
withName("User"),
(...)
)();
const Company = compose(
withName("Company"),
(...)
)();
You can then use the hasName
and getName
functions in order to determine if a name was assigned to the passed value, or to get the actual name that was assigned, respectively.
import { withName, hasName, getName } from "@commodo/name";
import { compose } from "ramda";
const User = compose(
withName("User"),
(...)
)();
const Unknown = compose(
(...)
)();
console.log(hasName(User));
console.log(getName(User));
console.log(hasName(Unknown));
console.log(getName(Unknown));
const user = new User();
console.log(hasName(user));
console.log(getName(user));
const unknown = new Unknown();
console.log(hasName(unknown));
console.log(getName(unknown));
Reference
withName(name: string): Function
Decorates a function (and its instances) with a name.
hasName(value: any): boolean
Checks if the passed value has a name assigned via the withName
.
getName(value: any): string
Returns a name assigned to the passed value. Returns empty string if none assigned.